package com.jie.controller;

import com.alibaba.fastjson.JSONObject;
import com.jie.annotation.Decrypt;
import com.jie.encryption.bean.EncryptBean;
import com.jie.encryption.utils.AesUtil;
import com.jie.encryption.utils.RsaUtil;
import com.jie.response.RespBean;
import com.jie.vo.BodyDimensionVO;
import com.jie.vo.ClockPlanVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.StandardCharsets;

import static org.bouncycastle.asn1.x509.ObjectDigestInfo.publicKey;

/**
 * key的接口
 * @Author: 杰
 * @Project :net-disk
 * @DateTime: 2024/5/28 13:51
 **/
@Api(tags = "密钥管理")
@RestController("/key")
public class EncryptKeyController {

    @ApiOperation(value = "获取RSA公钥", notes = "获取RSA密钥")
    @GetMapping("/getKey")
    public RespBean<?> getKey(){
        // 获取密钥的逻辑
        String publicKey = RsaUtil.getPublicKey();
        return RespBean.success("获取成功",publicKey);
    }
    @ApiOperation(value = "模拟前端")
    @PostMapping("/getAesKey")
    public RespBean<?> getAesKey(@RequestBody ClockPlanVO clockPlanVO){
        //公钥的ase
        String key = AesUtil.getKey();
        EncryptBean encryptBean;
        //修改
        try {
            //将ase进行加密
            String aesData = RsaUtil.encryptByPublicKey(key, RsaUtil.getPublicKey());
            //转为json
            String message = JSONObject.toJSONString(clockPlanVO);
            String rsaData = AesUtil.encrypt(message, key);
            encryptBean = EncryptBean.builder().rsaData(rsaData).aesData(aesData).build();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return RespBean.success("获取成功",encryptBean);
    }
}
